#include "uploadvideopage.h"
#include "ui_uploadvideopage.h"
#include "video_playback.h"
#include "util.h"
#include <QFileDialog>
#include "./model/datacenter.h"

UploadVideoPage::UploadVideoPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::UploadVideoPage)
{
    ui->setupUi(this);
    // 获取所有的分类数据
    auto dataCenter = model::DataCenter::getInstance();
    auto KindAndTagPtr = dataCenter->getKindAndTagClassPtr();
    ui->kinds->addItems(KindAndTagPtr->getAllKinds());
    ui->kinds->setCurrentIndex(-1);

    connect(ui->commitBtn, &QPushButton::clicked, this, &UploadVideoPage::onCommitBtnClicked);
    connect(ui->videoTitle, &QLineEdit::textChanged, this, &UploadVideoPage::onLineEditTextChanged);
    connect(ui->plainTextEdit, &QPlainTextEdit::textChanged, this, &UploadVideoPage::onPlainEditTextChanged);
    // 更换封面
    connect(ui->changeButton, &QPushButton::clicked, this, &UploadVideoPage::onChangeButtonClicked);
    connect(ui->kinds, &QComboBox::currentTextChanged, this, &UploadVideoPage::onUpdateTags);
}

UploadVideoPage::~UploadVideoPage()
{
    delete ui;
}

void UploadVideoPage::onCommitBtnClicked()
{
    emit switchMyselfPage(MyselfPage);
}

void UploadVideoPage::onLineEditTextChanged(const QString &text)
{
    // 先要从wordLfet中获取最大的输入字数，检测是否超过wordLeft
    QString wordLeftText = ui->wordLeft->text();

    int linePos = wordLeftText.indexOf('/');
    int totalCount = wordLeftText.mid(linePos + 1).toInt();

    // 没有超过，将text的长度和最大字数拼接起来
    int wordCount = text.length();
    if(wordCount <= totalCount)
    {
        ui->wordLeft->setText(QString::number(wordCount) + "/" + QString::number(totalCount));
    }
    else
    {
        LOG() << "视频长度超过80个字";
        ui->wordLeft->setText(text.mid(0, 80));
    }
}

void UploadVideoPage::onPlainEditTextChanged()
{
    // 获取QPlainTextEdit中的文本
    QString text = ui->plainTextEdit->toPlainText();
    int wordCount = text.length();

    QString wordLeft = ui->briefLeftWord->text();
    int linePos = wordLeft.indexOf('/');
    QString totalCount = wordLeft.mid(linePos + 1);
    if(wordCount <= totalCount.toInt())
    {
        ui->briefLeftWord->setText(QString::number(wordCount) + "/" + totalCount);
    }
    else
    {
        ui->plainTextEdit->setPlainText(text.mid(0, totalCount.toInt()));

        // 设置滚动条一直在底部
        // textCursor: 获取当前光标的位置
        // movePosition: 移动光标
        // QTextCursor::End: 将光标移动到文本的末尾
        // QTextCursor::MoveAnchor 光标在移动时，光标的位置是固定的，不会创建一个选取
        QTextCursor textCursor = ui->plainTextEdit->textCursor();
        textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        ui->plainTextEdit->setTextCursor(textCursor);
    }
}

void UploadVideoPage::onChangeButtonClicked()
{
    QString fileName = QFileDialog::getOpenFileName(nullptr, "选择视频封面图", "", "Images (*.png *.jpg *.xpm)");
    if(!fileName.isEmpty())
    {
        QPixmap pixmap(fileName);
        pixmap = pixmap.scaled(ui->imageLabel->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        ui->imageLabel->setPixmap(pixmap);
        repaint();
    }
}

void UploadVideoPage::onUpdateTags(const QString &kind)
{
    addTagsByKind(kind);
}

void UploadVideoPage::addTagsByKind(const QString &kind)
{
    // 1. 清除掉之前的按钮
    QList<QPushButton*> tagBtnList = ui->tagWidget->findChildren<QPushButton*>();
    for(auto tagBtn : tagBtnList)
    {
        ui->tagLayout->removeWidget(tagBtn);
        delete tagBtn;
    }
    // 获取到空白间距并将其删除
    QLayoutItem* spaceItem = ui->tagLayout->itemAt(ui->tagLayout->count() - 1);
    ui->tagLayout->removeItem(spaceItem);
    // 2. 读取当前分类对应的标签
    if(kind.isEmpty())
    {
        return;
    }
    auto dataCenter = model::DataCenter::getInstance();
    auto kindAndTagPtr = dataCenter->getKindAndTagClassPtr();
    auto tags = kindAndTagPtr->getTagsByKind(kind).keys();
    // 3. 添加标签按钮到layout
    for(auto& tag : tags)
    {
        QPushButton* tagBtn = new QPushButton(ui->tagWidget);
        tagBtn->setFixedSize(98, 49);
        tagBtn->setText(tag);
        tagBtn->setCheckable(true);
        tagBtn->setStyleSheet("QPushButton{"
                              "border : 1px solid #3ECEFE;"
                              "border-radius : 4px;"
                              "color : #3ECEFE;"
                              "font-family : 微软雅⿊;"
                              "font-size : 16px;"
                              "background-color : #FFFFFF;}"
                              "QPushButton:checked{"
                              "background-color : #3ECEFE;"
                              "color : #FFFFFF;}"
                              "QPushButton:unchecked{"
                              "background-color : #FFFFFF;"
                              "color : #3ECEFE;}");
        ui->tagLayout->addWidget(tagBtn);
    }
    // 在tagLayout最后放⼀个空⽩间距，将按钮挤到左侧
    ui->tagLayout->insertSpacing(tags.size(), ui->tagContent->width() - (98+20)*tags.size());
    ui->tagLayout->setSpacing(20);
}
